/*
  学习目标：优化组件更新
  需求: 只有count变化时, Child更新
*/

import React, { Component } from 'react';

export default class App extends Component {
  state = {
    count: 0,
    msg: 'hello React',
  };

  render() {
    const { count, msg } = this.state;
    return (
      <div>
        <h1>{count}</h1>
        <button onClick={() => this.setState({ count: count + 1 })}>+1</button>
        <h1>{msg}</h1>
        <button onClick={() => this.setState({ msg: msg + '~' })}>+~</button>
        <Child count={count} />
      </div>
    );
  }
}

class Child extends React.Component {
  // 作用: 根据返回值, 决定组件是否要更新
  shouldComponentUpdate(nextProps) {
    if (nextProps.count !== this.props.count) {
      return true;
    } else {
      return false;
    }
  }

  render() {
    return <div>我是一个写死内容的CHild - {this.props.count}</div>;
  }

  componentDidUpdate() {
    console.log('更新后执行了  ----->  ');
  }
}
